package servlet;

import hibernate.Role;
import hibernate.User;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import DAO.UserDAO;

/**
 * Servlet implementation class getUserInfo
 */
public class getUserInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public getUserInfo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		String username = session.getAttribute("userName").toString();
		PrintWriter out = response.getWriter();
		UserDAO u = new UserDAO();

	    @SuppressWarnings("unchecked")
	    List list = u.searchUser(username);
	    
		String s = "";
		User user;
		String RoleOfUser;
		for(int i=0;i<list.size();i++)
		{   
			user = ((User)list.get(i));
			Set<Role> sr = user.getRoles();
		    Iterator sit = sr.iterator();
		    RoleOfUser="";
		    for(;sit.hasNext();)
		    {
		    	RoleOfUser += ((Role)sit.next()).getName()+" ";
		    }
			s += "{userName:" + "'" + user.getUserName() + "',";
			s += "passWord:" + "'" + user.getPassword() + "',";
			s += "trueName:" + "'" + user.getTrueName() + "',";
			s += "phone:" + "'" + user.getPhone() + "',";
			s += "unit:" + "'" + user.getUnit() + "',";
			s += "job:" + "'" + user.getJob() + "',";
			s += "email:" + "'" + user.getEmail() + "',";
			s += "workPhone:" + "'" + user.getWorkPhone() + "',";
			s += "mobilePhone:" + "'" + user.getMobilePhone() + "',";
			s += "belongtoRole:" + "'" +RoleOfUser+ "'}";	
		}
        out.print(s);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}
}
